package top.dbpanda.cmsjpa.learn.thread.juc;

import java.util.concurrent.DelayQueue;

/**
 * @author 20018954
 */
public class Natatorium implements Runnable {
    private DelayQueue<Swimmer> queue = new DelayQueue<>();
    private volatile boolean isOpen = true;

    public void addSwimmer(String name, int playTime) {
        long endTime = System.currentTimeMillis() + playTime ;
        Swimmer swimmer = new Swimmer(name, endTime);
        System.out.println(swimmer.getName() + "进入游泳馆，可供游泳时间：" + playTime + "分");
        this.queue.add(swimmer);
    }

    @Override
    public void run() {
        while (isOpen) {
            try {
                Swimmer take = queue.take();
                System.out.println(take.getName() + "游泳时间结束");
                if (queue.size() == 0) {
                    isOpen = false;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
